package br.ufal.ic.dao;

import br.ufal.ic.util.HibernateUtil;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;

/**
 *
 * @author Marcos Paulo
 */
public abstract class GenericHibernateDAO<E, ID extends Serializable>
        implements AbstractDAO<E, ID> {

    private Class<E> persistentClass;

    public GenericHibernateDAO(Class<E> persistentClass) {
        this.persistentClass = persistentClass;
    }

    public void save(E e) {
        getSession().save(e);
    }

    public void delete(E e) {
        getSession().delete(e);
    }

    public void update(E e) {
        getSession().update(e);
    }

    public List<E> findAll() {
        Criteria criteria = getSession().createCriteria(persistentClass);
        return criteria.list();
    }

    public E findById(ID id) {
        return (E) getSession().get(persistentClass, id);
    }

    public Session getSession() {
        return HibernateUtil.getCurrentSession();
    }
}
